package com.xr.blog.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xr.blog.pojo.BlogComment;
import com.xr.blog.service.BlogCommentService;
import com.xr.blog.service.BlogConfigService;
import com.xr.blog.tools.Result;
import com.xr.blog.tools.constant.Code;
import com.xr.blog.tools.enums.CommentStatusEnum;
import com.xr.blog.tools.enums.CommentTargetTypeEnum;
import com.xr.blog.tools.ip.IpUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * 前台评论控制器
 * @author 幸仁杰
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private BlogCommentService commentService;

    @Autowired
    private BlogConfigService configService;

    /**
     * 加载评论
     * @param current 需要加载的页数
     * @param size 需要加载的数量
     * @param articleId 文章ID
     * @return
     */
    @PostMapping(value = "/more",produces = "application/json;charset=UTF-8")
    public List<BlogComment> commentPage(int current, int size, long articleId){
        return commentService.findPageByArticleId(new Page<>(current,size,false),articleId).getRecords();
    }


    /**
     * 保存或更新评论
     * @param comment 评论的内容
     * @param request
     * @return
     */
    @PostMapping(value = {"", "/save"},produces = "application/json;charset=UTF-8")
    public Result<BlogComment> save(@RequestBody @Validated(Insert.class) BlogComment comment, HttpServletRequest request) {
        if (comment.getTargetType().equals(CommentTargetTypeEnum.COMMENT.getValue())) {
            if (Objects.isNull(comment.getParentId())) {
                BlogComment targetComment = commentService.getById(comment.getTargetId());
                comment.setReplyUserName(targetComment.getAuthor());
            } else {
                BlogComment parentComment = commentService.getById(comment.getParentId());
                comment.setReplyUserName(parentComment.getAuthor());
            }
        }
        if (Objects.isNull(comment.getId())) {
            Boolean isCheck = configService.getConfigObject(Code.COMMENT_CHECK, Boolean.class);
            comment.setStatus(isCheck ? CommentStatusEnum.CHECKING.getValue() : CommentStatusEnum.PUBLISHED.getValue());
            comment.setUserAgent(request.getHeader("user-agent"));
            comment.setIp(IpUtils.getIpAddr(request));
            if(StringUtils.isEmpty(comment.getAuthor())){
                Random random = new Random();
                int num=random.nextInt(100000)+1;
                comment.setAuthor("松鼠"+num);
            }
            if(StringUtils.isEmpty(comment.getEmail())){
                comment.setEmailMd5("");
            }else{
                comment.setEmailMd5(DigestUtils.md5Hex(comment.getEmail()));
            }
            commentService.save(comment);
        } else {
            commentService.updateById(comment);
        }
        //commentService.clearCache();
        return Result.of(Code.SC_OK.getState(),"操作成功",comment);
    }
}
